﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpDrawCircleModelLineCmd.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"
#include "CmdStatusUtils.h"
#include "ActionDrawCircleElementInput.h"
#include "ActionDrawCircleElementCreator.h"
#include "GbmpSnapEndPointPriorityTypesPreprocessor.h"
#include "AddGbmpDynamicPannelUtils.h"
#include "GbmpControlIds.h"
#include "IUserTransaction.h"
#include "IModelView.h"
#include "GbmpUiPlatformUtils.h"
#include "Vector2dUtils.h"
#include "IModelLine.h"
#include "IArc2d.h"
#include "IElementBasicInformation.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(GbmpDrawCircleModelLineCmd)

GbmpDrawCircleModelLineCmd::GbmpDrawCircleModelLineCmd()
    : GbmpCommandBase(ID_CMD_GBMP_DRAW_CIRCLE_MODEL_LINE)
{
}

GbmpDrawCircleModelLineCmd::~GbmpDrawCircleModelLineCmd()
{
}

bool GbmpDrawCircleModelLineCmd::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

bool GbmpDrawCircleModelLineCmd::IsEnabled() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

OwnerPtr<IAction> GbmpDrawCircleModelLineCmd::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    ActionDrawCircleElementInput actionInput;
    // 定制Action
    actionInput.CommandId = GetCommandId();
    actionInput.CircleFinishedCallback = actionInput.CreateOnCircleFinishedCallback(&GbmpDrawCircleModelLineCmd::OnCircleFinished, this);

    std::vector<Int64> priorityTypes;
    priorityTypes.emplace_back(typeid(IModelLine).hash_code());
    actionInput.SnapCandidatesPreprocessor = NEW_AS_OWNER_PTR(GbmpSnapEndPointPriorityTypesPreprocessor, priorityTypes);

    OwnerPtr<IAction> opAction = ActionDrawCircleElementCreator::Create(actionInput);

    // 开启动态上下文面板
    AddGbmpDynamicPannelUtils::SetupModelLineCreationContext(opAction.get(), ID_PAGE_GBMP_MODIFY);

    return TransferOwnership(opAction);
}

ElementId GbmpDrawCircleModelLineCmd::OnCircleFinished(gcmp::IUiView* pCurrentView, const gcmp::Vector3d& centerPt, const gcmp::Vector3d& onCirclePt)
{
    DBG_WARN_AND_RETURN_UNLESS(pCurrentView, ElementId::InvalidID, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, ElementId::InvalidID, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_UNLESS(pModelView, ElementId::InvalidID, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制圆模型线"));
    DBG_WARN_AND_RETURN_UNLESS(opTransaction.get(), ElementId::InvalidID, L"opTransaction为空",L"GDMPLab",L"2024-03-30");

    // 将三维坐标点转化为平面上的二维点
    ElementId planeId = pModelView->GetWorkPlaneId();
    DBG_WARN_AND_RETURN_UNLESS(planeId.IsValid(), ElementId::InvalidID, L"planeId无效",L"GDMPLab",L"2024-03-30");
    Coordinate3d planeCoord = GbmpUiPlatformUtils::GetPlaneCoordinate3d(pDoc, planeId);
    Vector2d centerPtLocal = planeCoord.GetLocalPointXY(centerPt);
    Vector2d onCirclePtLocal = planeCoord.GetLocalPointXY(onCirclePt);
    double radius = Vector2dUtils::Distance(centerPtLocal, onCirclePtLocal);

    OwnerPtr<IArc2d> opCircle = IArc2d::Create(centerPtLocal, radius, 0, gcmp::Constants::MATH_2PI);
    IModelLine* pModelLine = IModelLine::Create(pDoc, planeId, TransferOwnership(opCircle), ElementCreationOptions::Normal);

    // 圆可能创建失败
    if (pModelLine == nullptr)
    {
        opTransaction->Rollback();
        return ElementId::InvalidID;
    }
    else
    {
        opTransaction->Commit();
        ElementId modelLineId = pModelLine->GetOwnerElement()->GetBasicInformation()->GetElementId();
        DBG_WARN_AND_RETURN_UNLESS(modelLineId.IsValid(), ElementId::InvalidID, L"modelLineId无效",L"GDMPLab",L"2024-03-30");
        return modelLineId;
    }
}
